package com.jing.weibo.controller.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jing.system.comm.BaseController;
import com.jing.system.utils.StringUtil;
import com.jing.weibo.pojo.system.Stationletter;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.system.StationletterService;
import com.jing.weibo.service.user.UserInfoService;

/**
 * 站内信的Controller
 * @author jing.yue
 * @version 2012/11/14 1.0.0
 */
@Controller
public class StationletterController extends BaseController {

	private static final Logger logger = Logger.getLogger(StationletterController.class);

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private StationletterService stationletterService;

	/**
	 * 跳转到我的站内信
	 * @param request
	 * @return
	 */
	@RequestMapping("/toMyStationletter")
	public String toMyStationletter(HttpServletRequest request) {
		return "user/system/mystationletter_manager";
	}

	/**
	 * 获取我接收的站内信
	 * @return
	 */
	@RequestMapping(value = "/stationletter/json/findReceStationletter")
	@ResponseBody
	public ModelMap findReceStationletter(HttpServletRequest request, Stationletter stationletter) {
		String result = null;
		Map<String, Object> map = null;
		UserInfo userInfo = getSessionUser(request);
		try {
			stationletter.setReceusername(userInfo.getUsername());
			stationletter.setReceisdel(0);
			map = stationletterService.findStationletter(stationletter);
			result = SUCCESS;
		} catch (Exception e) {
			logger.error(userInfo.getNickname() + ": 获取我接收的站内信异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", map);
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 获取我发送的站内信
	 * @return
	 */
	@RequestMapping(value = "/stationletter/json/findSendStationletter")
	@ResponseBody
	public ModelMap findSendStationletter(HttpServletRequest request, Stationletter stationletter) {
		String result = null;
		Map<String, Object> map = null;
		UserInfo userInfo = getSessionUser(request);
		try {
			stationletter.setSendusername(userInfo.getUsername());
			stationletter.setSendisdel(0);
			map = stationletterService.findStationletter(stationletter);
			result = SUCCESS;
		} catch (Exception e) {
			logger.error(userInfo.getNickname() + ": 获取我发送的站内信异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", map);
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 查看我的站内信
	 * @return
	 */
	@RequestMapping(value = "/stationletter/json/lockStationletter")
	@ResponseBody
	public ModelMap lockStationletter(HttpServletRequest request, Stationletter stationletter) {
		String result = null;
		UserInfo userInfo = getSessionUser(request);
		try {
			stationletter = stationletterService.findById(stationletter.getId());
			//接收用户是第一次查看站内信
			if(stationletter.getIssee().intValue() == 0 && stationletter.getReceusername().equals(userInfo.getUsername())) {
				//根据ID修改查看状态为已查看
				stationletterService.updateSeeStationletter(stationletter.getId());
			}
			result = SUCCESS;
		} catch (Exception e) {
			logger.error(userInfo.getNickname() + ": 获取我发送的站内信异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", stationletter);
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 发送站内信
	 * @return
	 */
	@RequestMapping(value = "/stationletter/json/sendStationletter")
	@ResponseBody
	public ModelMap sendStationletter(HttpServletRequest request, Stationletter stationletter) {
		String result = null;
		Integer errorNum = 0;
		Integer successNum = 0;
		UserInfo userInfo = getSessionUser(request);
		try {
			//处理多个接收用户
			String[] receusernames = stationletter.getReceusername().split(";");
			for (String receusername : receusernames) {
				if(StringUtil.isNotEmpty(receusername)) {
					try {
						stationletterService.saveStationletter(stationletter.getTitle(), stationletter.getContent(), receusername, userInfo.getUsername());
						successNum ++;
					} catch (Exception e) {
						logger.error(String.format("%s: 给[%s]发送站内信异常!", userInfo.getNickname(), receusername), e);
						errorNum ++;
					}
				}
			}
			result = SUCCESS;
		} catch (Exception e) {
			logger.error(userInfo.getNickname() + ": 发送站内信异常!", e);
			result = ERROR;
		}
		String sendMsg = null;
		if(errorNum > 0) {
			sendMsg = String.format("您成功发送了[%s]封站内信~ [%s]封站内信发送失败!", successNum, errorNum);
		} else if(successNum > 0) {
			sendMsg = String.format("您成功发送了[%s]封站内信~", successNum);
		} else {
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("sendMsg", sendMsg);
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 根据用户昵称模糊查询好友信息
	 * @return
	 */
	@RequestMapping(value = "/stationletter/json/findByNicknameUserInfo")
	@ResponseBody
	public ModelMap findByNicknameUserInfo(HttpServletRequest request, UserInfo queryUser) {
		String result = null;
		UserInfo userInfo = getSessionUser(request);
		Map<String, Object> map = null;
		try {
			map = userInfoService.findByNicknameUserInfo(queryUser);
			result = SUCCESS;
		} catch (Exception e) {
			logger.error(userInfo.getNickname() + ": 获取我发送的站内信异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", map);
		modelMap.put("result", result);
		return modelMap;
	}
}